import { create } from 'zustand'
import { devtools } from 'zustand/middleware'

// 模态框类型
export type ModalType = 
  | 'newProject'
  | 'editProject'
  | 'deleteProject'
  | 'newChapter'
  | 'editChapter'
  | 'deleteChapter'
  | 'newCharacter'
  | 'editCharacter'
  | 'deleteCharacter'
  | 'settings';

// 模态框数据
export interface ModalData {
  [key: string]: any;
}

// UI状态
export interface UIState {
  // 侧边栏状态
  isSidebarOpen: boolean;
  isRightSidebarOpen: boolean;
  
  // 模态框
  activeModal: ModalType | null;
  modalData: ModalData;
  
  // 拖拽状态
  isDragging: boolean;
  dragSource: string | null;
  
  // AI助手面板
  isAIAssistantOpen: boolean;
  
  // 操作函数
  toggleSidebar: () => void;
  toggleRightSidebar: () => void;
  openModal: (type: ModalType, data?: ModalData) => void;
  closeModal: () => void;
  setDragging: (isDragging: boolean, source?: string | null) => void;
  toggleAIAssistant: () => void;
}

// 初始状态
const initialState = {
  isSidebarOpen: true,
  isRightSidebarOpen: false,
  activeModal: null,
  modalData: {},
  isDragging: false,
  dragSource: null,
  isAIAssistantOpen: false
};

export const useUIStore = create<UIState>()(
  devtools((set) => ({
    ...initialState,
    
    toggleSidebar: () => set(state => ({ isSidebarOpen: !state.isSidebarOpen })),
    
    toggleRightSidebar: () => set(state => ({ isRightSidebarOpen: !state.isRightSidebarOpen })),
    
    openModal: (type, data = {}) => set({ activeModal: type, modalData: data }),
    
    closeModal: () => set({ activeModal: null, modalData: {} }),
    
    setDragging: (isDragging, source = null) => set({ isDragging, dragSource: source }),
    
    toggleAIAssistant: () => set(state => ({ isAIAssistantOpen: !state.isAIAssistantOpen }))
  }))
); 